﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace 生产计划批量打印工具
{
    public partial class frmOrderChangesH : Form
    {
        string strConnectionString = "Data Source=@ServerAddress;Initial Catalog=TPAMain;Persist Security Info=True;User ID=sa;Password=856518armyman";
        string strDefaultSelectStringA = "select printed 打印标记,bga001 变更单号,bga002 变更日期,bga007 订单编号,bga012 原合同号,bga013 新合同号,bga014 原验收方式,bga015 新验收方式,bga020 原开票形式,bga021 新开票形式,bga033 原备注,bga034 新备注,bga035 变更单备注 from orbgah_table,hta_table where hta001=bga007 and hta026='@生产车间' and bga026='Y' and (bga012<>bga013 or bga014<>bga015 or bga020<>bga021 or bga033 not like bga034 or bga001 in (select bgb001 from orbgbh_table where (bgb005<>bgb006 or bgb008<>bgb009 or bgb014<>bgb015 or bgb016<>bgb017 or bgb018<>bgb019 or bgb020<>bgb021 or bgb024<>bgb025 or bgb026<>bgb027 or bgb028<>bgb029)))";
        string strDefaultSelectStringB = "select bgb002 变更单项次,bgb004 订单项次,bgb005 原产品编号,bgb006 新产品编号,bgb008 原数量,bgb009 新数量,bgb014 原单价,bgb015 新单价,bgb016 原交货日期,bgb017 新交货日期,bgb018 原执行标准,bgb019 新执行标准,bgb020 原附加协议,bgb021 新附加协议,bgb022 原用户计划号,bgb023 新用户计划号,bgb024 原单项验收方式,bgb025 新单项验收方式,bgb026 原订单单项备注,bgb027 新订单单项备注,bgb028 原产品单项备注,bgb029 新产品单项备注 from orbgbh_table where bgb001='@BGNo' order by bgb002";
        string strUpdatePrintMark = "update orbgah_table set printed=1 where bga001='@BGNo'";
        
        SqlTools.SqlTools sqt;

        string strFactoryName;

        public frmOrderChangesH(string strTmpFactoryName)
        {
            InitializeComponent();
            strFactoryName = strTmpFactoryName;
            strDefaultSelectStringA = strDefaultSelectStringA.Replace("@生产计划", strFactoryName);
        }

        private void rbtn1_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn1.Checked)
            {
                rbtn2.Checked = false;
                rbtn3.Checked = false;
                rbtn4.Checked = false;
            }
            txtNoFrom.Enabled = rbtn1.Checked;
            txtNoTo.Enabled = rbtn1.Checked;
        }

        private void rbtn2_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn2.Checked)
            {
                rbtn1.Checked = false;
                rbtn3.Checked = false;
                rbtn4.Checked = false;
            }
            dtpDateFrom.Enabled = rbtn2.Checked;
            dtpDateTo.Enabled = rbtn2.Checked;
        }

        private void rbtn3_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn3.Checked)
            {
                rbtn1.Checked= false;
                rbtn2.Checked = false;
                rbtn4.Checked = false;
            }
            txtContractNo.Enabled = rbtn3.Checked;
        }


        private void rbtn4_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtn4.Checked)
            {
                rbtn1.Checked = false;
                rbtn2.Checked = false;
                rbtn3.Checked = false;
            }
        }

        private void btnPrintSetting_Click(object sender, EventArgs e)
        {
            frmPrintSettings newFrmPrintSettings = new frmPrintSettings();
            newFrmPrintSettings.ShowDialog();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            dgv1.DataSource = null;
            dgv2.DataSource = null;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            int iWorkMode = 0;
            string strSelectString = strDefaultSelectStringA.Replace ("@生产车间",strFactoryName );
            if (rbtn1.Checked)
            {
                iWorkMode = 1;
            }
            if (rbtn2.Checked)
            {
                iWorkMode = 2;
            }
            if (rbtn3.Checked)
            {
                iWorkMode = 3;
            }
            switch (iWorkMode)
            {
                case 1:
                    if (txtNoFrom.Text.Trim().Length == 0 || txtNoTo.Text.Trim().Length == 0)
                    {
                        MessageBox.Show("起始变更单编号和终止变更单编号不能为空！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        txtNoFrom.Focus();
                        return;
                    }
                    strSelectString += " and bga001 between '" + txtNoFrom.Text.Trim() + "' and '" + txtNoTo.Text.Trim() + "'";
                    break;
                case 2:
                    if (dtpDateFrom.Value == dtpDateTo.Value)
                    {
                        strSelectString += " and bga002='" + dtpDateFrom.Value.ToString("yyyyMMdd") + "'";
                    }
                    else
                    {
                        strSelectString += " and bga002 between '" + dtpDateFrom.Value.ToString("yyyyMMdd") + "' and '" + dtpDateTo.Value.ToString("yyyyMMdd") + "'";
                    }
                    break;
                case 3:
                    strSelectString += " and (bga012 like '" + txtContractNo.Text.Trim() + "' or bga013 like '"+txtContractNo .Text .Trim ()+"')";
                    break;
            }
            if (chkSearchUnprintRecord.Checked)
            {
                strSelectString += " and printed=0";
            }
            dgv1.DataSource = sqt.GetDataTable(strSelectString);
            dgv1.AutoResizeColumns();
            if (dgv1.Rows.Count > 0)
            {
                dgv1.CurrentCell = dgv1.Rows[0].Cells[1];
                for (int i = 0; i < dgv1.Rows.Count; i++)
                {
                    if (GetString(dgv1.Rows[i].Cells["原备注"].Value) != GetString(dgv1.Rows[i].Cells["新备注"].Value))
                    {
                        dgv1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    }
                    if (GetString(dgv1.Rows[i].Cells["原验收方式"].Value) != GetString(dgv1.Rows[i].Cells["新验收方式"].Value))
                    {
                        dgv1.Rows[i].DefaultCellStyle.BackColor = Color.Yellow ;
                    }
                }
            }
            else
            {
                dgv2.DataSource = null;
            }
        }

        private void frmOrderChangesH_Load(object sender, EventArgs e)
        {
            strConnectionString = strConnectionString.Replace("@ServerAddress", 生产计划批量打印工具.Properties.Settings.Default.ServerAddress);
            sqt = new SqlTools.SqlTools(strConnectionString);
        }

        private void dgv1_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
        {
            if (e.Column.Name == "打印标记" && e.Column.GetType ().Name!="DataGridViewCheckBoxColumn")
            {
                dgv1.Columns.Remove("打印标记");
                DataGridViewCheckBoxColumn dgvcbc = new DataGridViewCheckBoxColumn();
                dgvcbc.Name = "打印标记";
                dgvcbc.HeaderCell.Value = "打印标记";
                dgvcbc.DataPropertyName = "打印标记";
                dgv1.Columns.Add(dgvcbc);
            }
        }

        private void dgv1_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgv1.CurrentRow != null)
            {
                if (GetString(dgv1.CurrentRow.Cells["变更单号"].Value).Length > 0)
                {
                    dgv2.DataSource = sqt.GetDataTable(strDefaultSelectStringB.Replace("@BGNo", GetString(dgv1.CurrentRow.Cells["变更单号"].Value)));
                    dgv2.AutoResizeColumns();
                }
            }
        }

        private string GetString(object tmpobj)
        {
            try
            {
                return tmpobj.ToString().Trim();
            }
            catch
            {
                return string.Empty;
            }
        }

        private int GetInt(object tmpobj)
        {
            try
            {
                return Convert.ToInt32(tmpobj);
            }
            catch
            {
                return 0;
            }
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                if (dgv1.SelectedRows.Count > 1)
                {
                    MessageBox.Show("预览仅显示第一个计划！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                ReportDocument rd = new ReportDocument();
                try
                {
                    rd.Load(Application.StartupPath + "\\订单变更单.rpt");
                    TextObject to;
                    DataRow r1, r2;
                    DataSet ds = new DataSet();
                    DataTable dtHeader = new DataTable();
                    dtHeader.TableName = "ReportDataHeader";
                    dtHeader.Columns.Add("标题");
                    dtHeader.Columns.Add("变更单号");
                    dtHeader.Columns.Add("变更日期");
                    dtHeader.Columns.Add("订单编号");
                    dtHeader.Columns.Add("开票形式");
                    dtHeader.Columns.Add("验收方式");
                    dtHeader.Columns.Add("合同号");
                    dtHeader.Columns.Add("打印日期");
                    dtHeader.Columns.Add("打印人");
                    dtHeader.Columns.Add("订单备注");
                    dtHeader.Columns.Add("变更单备注");
                    DataTable dtBody = new DataTable();
                    dtBody.TableName = "ReportDataBody";
                    dtBody.Columns.Add("变更单号");
                    dtBody.Columns.Add("变更单项次");
                    dtBody.Columns.Add("订单项次");
                    dtBody.Columns.Add("型号规格");
                    dtBody.Columns.Add("数量");
                    dtBody.Columns.Add("变更描述");
                    //增加订单单头
                    r1 = dtHeader.NewRow();
                    r1["标题"] ="*"+GetString(dgv1.SelectedRows[0].Cells["变更单号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[0].Cells["变更单号"].Value).Substring(4, 2) + "月生产计划销货后变更单";
                    r1["变更单号"] = GetString(dgv1.SelectedRows[0].Cells["变更单号"].Value);
                    r1["变更日期"] = GetString(dgv1.SelectedRows[0].Cells["变更单号"].Value).Substring(0, 8);
                    r1["订单编号"] = GetString(dgv1.SelectedRows[0].Cells["订单编号"].Value);
                    r1["打印日期"] = DateTime.Now.ToString("yyyy年MM月dd日");
                    r1["打印人"] = 生产计划批量打印工具.Properties.Settings.Default.Operator;
                    if (GetString(dgv1.SelectedRows[0].Cells["原开票形式"].Value) != GetString(dgv1.SelectedRows[0].Cells["新开票形式"].Value))
                    {
                        r1["开票形式"] = "原开票形式\"" + GetString(dgv1.SelectedRows[0].Cells["原开票形式"].Value) + "\"变更为\"" + GetString(dgv1.SelectedRows[0].Cells["新开票形式"].Value) + "\"";
                        to = (TextObject)rd.ReportDefinition.ReportObjects["Text4"];
                        to.ApplyFont(new Font("宋体", 11, FontStyle.Bold));
                    }
                    else
                    {
                        r1["开票形式"] = GetString(dgv1.SelectedRows[0].Cells["原开票形式"].Value);
                    }
                    if (GetString(dgv1.SelectedRows[0].Cells["原验收方式"].Value) != GetString(dgv1.SelectedRows[0].Cells["新验收方式"].Value))
                    {
                        r1["验收方式"] = "原验收方式\"" + GetString(dgv1.SelectedRows[0].Cells["原验收方式"].Value) + "\"变更为\"" + GetString(dgv1.SelectedRows[0].Cells["新验收方式"].Value) + "\"";
                        to = (TextObject)rd.ReportDefinition.ReportObjects["Text6"];
                        to.ApplyFont(new Font("宋体", 11, FontStyle.Bold));
                    }
                    else
                    {
                        r1["验收方式"] = GetString(dgv1.SelectedRows[0].Cells["原验收方式"].Value);
                    }
                    if (GetString(dgv1.SelectedRows[0].Cells["原合同号"].Value) != GetString(dgv1.SelectedRows[0].Cells["新合同号"].Value))
                    {
                        r1["合同号"] = "原合同号\"" + GetString(dgv1.SelectedRows[0].Cells["原合同号"].Value) + "\"变更为\"" + GetString(dgv1.SelectedRows[0].Cells["新合同号"].Value) + "\"";
                        to = (TextObject)rd.ReportDefinition.ReportObjects["Text7"];
                        to.ApplyFont(new Font("宋体", 11, FontStyle.Bold));
                    }
                    else
                    {
                        r1["合同号"] = GetString(dgv1.SelectedRows[0].Cells["原合同号"].Value);
                    }
                    if (GetString(dgv1.SelectedRows[0].Cells["原备注"].Value) != GetString(dgv1.SelectedRows[0].Cells["新备注"].Value))
                    {
                        r1["订单备注"] = "原备注\n\"" + GetString(dgv1.SelectedRows[0].Cells["原备注"].Value) + "\"\n变更为\n\"" + GetString(dgv1.SelectedRows[0].Cells["新备注"].Value) + "\"";
                        to = (TextObject)rd.ReportDefinition.ReportObjects["Text15"];
                        to.ApplyFont(new Font("宋体", 11, FontStyle.Bold));
                    }
                    else
                    {
                        r1["订单备注"] = GetString(dgv1.SelectedRows[0].Cells["原备注"].Value);
                    }
                    r1["变更单备注"] = GetString(dgv1.SelectedRows[0].Cells["变更单备注"].Value);
                    dtHeader.Rows.Add(r1);
                    //增加单身
                    DataTable tmpdt = sqt.GetDataTable(strDefaultSelectStringB.Replace("@BGNo", GetString(dgv1.SelectedRows[0].Cells["变更单号"].Value)));
                    if (tmpdt.Rows.Count > 0)
                    {
                        //bgb002 变更单项次,bgb004 订单项次,bgb005 原产品编号,bgb006 新产品编号,bgb008 原数量,bgb009 新数量,bgb014 原单价,bgb015 新单价,bgb016 原交货日期,bgb017 新交货日期,bgb018 原执行标准,bgb019 新执行标准,bgb020 原附加协议,bgb021 新附加协议,bgb022 原用户计划号,bgb023 新用户计划号,bgb024 原单项验收方式,bgb025 新单项验收方式,bgb026 原订单单项备注,bgb027 新订单单项备注,bgb028 原产品单项备注,bgb029 新产品单项备注
                        for (int k = 0; k < tmpdt.Rows.Count; k++)
                        {
                            int iCount = 1;
                            StringBuilder tmpsb = new StringBuilder();
                            r2 = dtBody.NewRow();
                            r2["变更单号"] = GetString(dgv1.SelectedRows[0].Cells["变更单号"].Value);
                            r2["变更单项次"] = GetString(tmpdt.Rows[k]["变更单项次"]);
                            r2["订单项次"] = GetString(tmpdt.Rows[k]["订单项次"]);
                            r2["型号规格"] = GetString(tmpdt.Rows[k]["原产品编号"]);
                            r2["数量"] = GetString(tmpdt.Rows[k]["新数量"]).Replace(".00", "");
                            if (GetString(tmpdt.Rows[k]["原产品编号"]) != GetString(tmpdt.Rows[k]["新产品编号"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原型号规格变更为：\"" + GetString(tmpdt.Rows[k]["新产品编号"]) + "\"");
                                iCount++;
                            }
                            if (GetString(tmpdt.Rows[k]["原数量"]) != GetString(tmpdt.Rows[k]["新数量"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原数量\"" + GetString(tmpdt.Rows[k]["原数量"]).Replace(".00", "") + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新数量"]).Replace(".00", "") + "\"(" + GetString(GetInt(GetString(tmpdt.Rows[k]["新数量"]).Replace(".00", "")) - GetInt(GetString(tmpdt.Rows[k]["原数量"]).Replace(".00", ""))) + ")");
                                iCount++;
                            }
                            if (GetString(tmpdt.Rows[k]["原单价"]) != GetString(tmpdt.Rows[k]["新单价"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原单价\"" + GetString(tmpdt.Rows[k]["原单价"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新单价"]) + "\"");
                                iCount++;
                            }
                            if (GetString(tmpdt.Rows[k]["原交货日期"]) != GetString(tmpdt.Rows[k]["新交货日期"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原交货日期\"" + GetString(tmpdt.Rows[k]["原交货日期"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新交货日期"]) + "\"");
                                iCount++;
                            }
                            if (GetString(tmpdt.Rows[k]["原执行标准"]) != GetString(tmpdt.Rows[k]["新执行标准"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原执行标准\"" + GetString(tmpdt.Rows[k]["原执行标准"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新执行标准"]) + "\"");
                                iCount++;
                            }
                            if (GetString(tmpdt.Rows[k]["原附加协议"]) != GetString(tmpdt.Rows[k]["新附加协议"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原附加协议\"" + GetString(tmpdt.Rows[k]["原附加协议"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新附加协议"]) + "\"");
                                iCount++;
                            }
                            if (GetString(tmpdt.Rows[k]["原用户计划号"]) != GetString(tmpdt.Rows[k]["新用户计划号"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原用户计划号\"" + GetString(tmpdt.Rows[k]["原用户计划号"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新用户计划号"]) + "\"");
                                iCount++;
                            }
                            if (GetString(tmpdt.Rows[k]["原单项验收方式"]) != GetString(tmpdt.Rows[k]["新单项验收方式"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原单项验收方式\"" + GetString(tmpdt.Rows[k]["原单项验收方式"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新单项验收方式"]) + "\"");
                                iCount++;
                            }
                            if (GetString(tmpdt.Rows[k]["原订单单项备注"]) != GetString(tmpdt.Rows[k]["新订单单项备注"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原订单单项备注\"" + GetString(tmpdt.Rows[k]["原订单单项备注"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新订单单项备注"]) + "\"");
                                iCount++;
                            }
                            if (GetString(tmpdt.Rows[k]["原产品单项备注"]) != GetString(tmpdt.Rows[k]["新产品单项备注"]))
                            {
                                tmpsb.AppendLine(iCount.ToString() + ".原产品单项备注\"" + GetString(tmpdt.Rows[k]["原产品单项备注"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新产品单项备注"]) + "\"");
                                iCount++;
                            }
                            r2["变更描述"] = tmpsb.ToString();
                            dtBody.Rows.Add(r2);
                        }
                    }
                    else
                    {
                        r2 = dtBody.NewRow();
                        r2["变更单号"] = GetString(dgv1.SelectedRows[0].Cells["变更单号"].Value);
                        dtBody.Rows.Add(r2);
                    }
                    ds.Tables.Add(dtHeader);
                    ds.Tables.Add(dtBody);
                    if (dtHeader.Rows.Count > 0)
                    {
                        rd.SetDataSource(ds);
                    }
                    if (rd != null)
                    {
                        frmReportPreview newFrmReportPreview = new frmReportPreview(rd);
                        newFrmReportPreview.WindowState = FormWindowState.Maximized;
                        newFrmReportPreview.ShowDialog();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                throw new Exception("未选择任何订单变更单！");
            }
        }

        private void frmOrderChangesH_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Owner.Show();
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            if (dgv1.Rows.Count > 0)
            {
                for (int i = 0; i < dgv1.Rows.Count; i++)
                {
                    ReportDocument rd = new ReportDocument();
                    try
                    {
                        rd.Load(Application.StartupPath + "\\订单变更单.rpt");
                        TextObject to;
                        DataRow r1, r2;
                        bool bPriceChanged = false;
                        DataSet ds = new DataSet();
                        DataTable dtHeader = new DataTable();
                        dtHeader.TableName = "ReportDataHeader";
                        dtHeader.Columns.Add("标题");
                        dtHeader.Columns.Add("变更单号");
                        dtHeader.Columns.Add("变更日期");
                        dtHeader.Columns.Add("订单编号");
                        dtHeader.Columns.Add("开票形式");
                        dtHeader.Columns.Add("验收方式");
                        dtHeader.Columns.Add("合同号");
                        dtHeader.Columns.Add("打印日期");
                        dtHeader.Columns.Add("打印人");
                        dtHeader.Columns.Add("订单备注");
                        dtHeader.Columns.Add("变更单备注");
                        DataTable dtBody = new DataTable();
                        dtBody.TableName = "ReportDataBody";
                        dtBody.Columns.Add("变更单号");
                        dtBody.Columns.Add("变更单项次");
                        dtBody.Columns.Add("订单项次");
                        dtBody.Columns.Add("型号规格");
                        dtBody.Columns.Add("数量");
                        dtBody.Columns.Add("变更描述");
                        //增加订单单头
                        r1 = dtHeader.NewRow();
                        r1["标题"] = "*" + GetString(dgv1.SelectedRows[0].Cells["变更单号"].Value).Substring(0, 4) + "年" + GetString(dgv1.SelectedRows[0].Cells["变更单号"].Value).Substring(4, 2) + "月生产计划销货后变更单";
                        r1["变更单号"] = GetString(dgv1.Rows[i].Cells["变更单号"].Value);
                        r1["变更日期"] = GetString(dgv1.Rows[i].Cells["变更单号"].Value).Substring(0, 8);
                        r1["订单编号"] = GetString(dgv1.Rows[i].Cells["订单编号"].Value);
                        r1["打印日期"] = DateTime.Now.ToString("yyyy年MM月dd日");
                        r1["打印人"] = 生产计划批量打印工具.Properties.Settings.Default.Operator;
                        if (GetString(dgv1.Rows[i].Cells["原开票形式"].Value) != GetString(dgv1.Rows[i].Cells["新开票形式"].Value))
                        {
                            r1["开票形式"] = "原开票形式\"" + GetString(dgv1.Rows[i].Cells["原开票形式"].Value) + "\"变更为\"" + GetString(dgv1.Rows[i].Cells["新开票形式"].Value) + "\"";
                            to = (TextObject)rd.ReportDefinition.ReportObjects["Text4"];
                            to.ApplyFont(new Font("宋体", 11, FontStyle.Bold));
                        }
                        else
                        {
                            r1["开票形式"] = GetString(dgv1.Rows[i].Cells["原开票形式"].Value);
                        }
                        if (GetString(dgv1.Rows[i].Cells["原验收方式"].Value) != GetString(dgv1.Rows[i].Cells["新验收方式"].Value))
                        {
                            r1["验收方式"] = "原验收方式\"" + GetString(dgv1.Rows[i].Cells["原验收方式"].Value) + "\"变更为\"" + GetString(dgv1.Rows[i].Cells["新验收方式"].Value) + "\"";
                            to = (TextObject)rd.ReportDefinition.ReportObjects["Text6"];
                            to.ApplyFont(new Font("宋体", 11, FontStyle.Bold));
                        }
                        else
                        {
                            r1["验收方式"] = GetString(dgv1.Rows[i].Cells["原验收方式"].Value);
                        }
                        if (GetString(dgv1.Rows[i].Cells["原合同号"].Value) != GetString(dgv1.Rows[i].Cells["新合同号"].Value))
                        {
                            r1["合同号"] = "原合同号\"" + GetString(dgv1.Rows[i].Cells["原合同号"].Value) + "\"变更为\"" + GetString(dgv1.Rows[i].Cells["新合同号"].Value) + "\"";
                            to = (TextObject)rd.ReportDefinition.ReportObjects["Text7"];
                            to.ApplyFont(new Font("宋体", 11, FontStyle.Bold));
                        }
                        else
                        {
                            r1["合同号"] = GetString(dgv1.Rows[i].Cells["原合同号"].Value);
                        }
                        if (GetString(dgv1.Rows[i].Cells["原备注"].Value) != GetString(dgv1.Rows[i].Cells["新备注"].Value))
                        {
                            r1["订单备注"] = "原备注\n\"" + GetString(dgv1.Rows[i].Cells["原备注"].Value) + "\"\n变更为\n\"" + GetString(dgv1.Rows[i].Cells["新备注"].Value) + "\"";
                            to = (TextObject)rd.ReportDefinition.ReportObjects["Text15"];
                            to.ApplyFont(new Font("宋体", 11, FontStyle.Bold));
                        }
                        else
                        {
                            r1["订单备注"] = GetString(dgv1.Rows[i].Cells["原备注"].Value);
                        }
                        if (GetString(dgv1.Rows[i].Cells["变更单备注"].Value).Trim().Length > 0)
                        {
                            to = (TextObject)rd.ReportDefinition.ReportObjects["Text10"];
                            to.ApplyFont(new Font("宋体", 11, FontStyle.Bold));
                        }
                        r1["变更单备注"] = GetString(dgv1.Rows[i].Cells["变更单备注"].Value);
                        dtHeader.Rows.Add(r1);
                        //增加单身
                        DataTable tmpdt = sqt.GetDataTable(strDefaultSelectStringB.Replace("@BGNo", GetString(dgv1.Rows[i].Cells["变更单号"].Value)));
                        if (tmpdt.Rows.Count > 0)
                        {
                            for (int k = 0; k < tmpdt.Rows.Count; k++)
                            {
                                int iCount = 1;
                                StringBuilder tmpsb = new StringBuilder();
                                r2 = dtBody.NewRow();
                                r2["变更单号"] = GetString(dgv1.Rows[i].Cells["变更单号"].Value);
                                r2["变更单项次"] = GetString(tmpdt.Rows[k]["变更单项次"]);
                                r2["订单项次"] = GetString(tmpdt.Rows[k]["订单项次"]);
                                r2["型号规格"] = GetString(tmpdt.Rows[k]["原产品编号"]);
                                r2["数量"] = GetString(tmpdt.Rows[k]["新数量"]).Replace(".00", "");
                                if (GetString(tmpdt.Rows[k]["原产品编号"]) != GetString(tmpdt.Rows[k]["新产品编号"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原型号规格变更为：\"" + GetString(tmpdt.Rows[k]["新产品编号"]) + "\"");
                                    iCount++;
                                }
                                if (GetString(tmpdt.Rows[k]["原数量"]) != GetString(tmpdt.Rows[k]["新数量"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原数量\"" + GetString(tmpdt.Rows[k]["原数量"]).Replace(".00", "") + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新数量"]).Replace(".00", "") + "\"(" + GetString(GetInt(GetString(tmpdt.Rows[k]["新数量"]).Replace(".00", "")) - GetInt(GetString(tmpdt.Rows[k]["原数量"]).Replace(".00", ""))) + ")");
                                    iCount++;
                                }
                                if (GetString(tmpdt.Rows[k]["原单价"]) != GetString(tmpdt.Rows[k]["新单价"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原单价\"" + GetString(tmpdt.Rows[k]["原单价"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新单价"]) + "\"");
                                    bPriceChanged = true;
                                    iCount++;
                                }
                                if (GetString(tmpdt.Rows[k]["原交货日期"]) != GetString(tmpdt.Rows[k]["新交货日期"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原交货日期\"" + GetString(tmpdt.Rows[k]["原交货日期"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新交货日期"]) + "\"");
                                    iCount++;
                                }
                                if (GetString(tmpdt.Rows[k]["原执行标准"]) != GetString(tmpdt.Rows[k]["新执行标准"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原执行标准\"" + GetString(tmpdt.Rows[k]["原执行标准"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新执行标准"]) + "\"");
                                    iCount++;
                                }
                                if (GetString(tmpdt.Rows[k]["原附加协议"]) != GetString(tmpdt.Rows[k]["新附加协议"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原附加协议\"" + GetString(tmpdt.Rows[k]["原附加协议"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新附加协议"]) + "\"");
                                    iCount++;
                                }
                                if (GetString(tmpdt.Rows[k]["原用户计划号"]) != GetString(tmpdt.Rows[k]["新用户计划号"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原用户计划号\"" + GetString(tmpdt.Rows[k]["原用户计划号"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新用户计划号"]) + "\"");
                                    iCount++;
                                }
                                if (GetString(tmpdt.Rows[k]["原单项验收方式"]) != GetString(tmpdt.Rows[k]["新单项验收方式"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原单项验收方式\"" + GetString(tmpdt.Rows[k]["原单项验收方式"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新单项验收方式"]) + "\"");
                                    iCount++;
                                }
                                if (GetString(tmpdt.Rows[k]["原订单单项备注"]) != GetString(tmpdt.Rows[k]["新订单单项备注"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原订单单项备注\"" + GetString(tmpdt.Rows[k]["原订单单项备注"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新订单单项备注"]) + "\"");
                                    iCount++;
                                }
                                if (GetString(tmpdt.Rows[k]["原产品单项备注"]) != GetString(tmpdt.Rows[k]["新产品单项备注"]))
                                {
                                    tmpsb.AppendLine(iCount.ToString() + ".原产品单项备注\"" + GetString(tmpdt.Rows[k]["原产品单项备注"]) + "\"变更为：\"" + GetString(tmpdt.Rows[k]["新产品单项备注"]) + "\"");
                                    iCount++;
                                }
                                r2["变更描述"] = tmpsb.ToString();
                                dtBody.Rows.Add(r2);
                            }
                        }
                        else
                        {
                            r2 = dtBody.NewRow();
                            r2["变更单号"] = GetString(dgv1.Rows[i].Cells["变更单号"].Value);
                            dtBody.Rows.Add(r2);
                        }
                        if (bPriceChanged)
                        {
                            dtHeader.Rows[0]["标题"] = dtHeader.Rows[0]["标题"].ToString() + "(单价变更)";
                        }
                        ds.Tables.Add(dtHeader);
                        ds.Tables.Add(dtBody);
                        if (dtHeader.Rows.Count > 0)
                        {
                            rd.SetDataSource(ds);
                        }
                        if (rd != null)
                        {
                            if (生产计划批量打印工具.Properties.Settings.Default.PrinterName.Length > 0)
                            {
                                rd.PrintOptions.PrinterName = 生产计划批量打印工具.Properties.Settings.Default.PrinterName;
                            }
                            rd.PrintToPrinter(GetInt(nudCopies.Value), true, 0, 0);
                            sqt.DoCmdExecuteNonQuery(strUpdatePrintMark.Replace("@BGNo", GetString(dgv1.Rows[i].Cells["变更单号"].Value)));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        rd.Dispose();
                    }
                }
            }
            else
            {
                throw new Exception("未选择任何订单变更单！");
            }
        }
    }
}